package com.sskj.givecoin.v.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.sskj.givecoin.R;
import com.sskj.givecoin.adapter.SpinnerTextColorAdapter;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.common.Constants;
import com.sskj.givecoin.m.bean.bean.OrderDetailBean;
import com.sskj.givecoin.p.activity.FabiSellDetail2Presenter;
import com.sskj.givecoin.utils.BottomUpUtil;
import com.sskj.givecoin.utils.ClickUtil;
import com.sskj.givecoin.utils.CopyUtil;
import com.sskj.givecoin.utils.DisposUtil;
import com.sskj.givecoin.utils.ImageUtil;
import com.sskj.givecoin.utils.ImgUtils;
import com.sskj.givecoin.utils.LanguageUtil;
import com.sskj.givecoin.utils.NumberUtil;
import com.sskj.givecoin.utils.PermissionTipUtil;
import com.sskj.givecoin.utils.RxSchedulersHelper;
import com.sskj.givecoin.utils.TipUtil;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.base.BaseActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

public class FabiSellDetail2Activity extends BaseActivity<FabiSellDetail2Presenter> {
    @BindView(R.id.btAppeal)
    Button btAppeal;
    @BindView(R.id.llBuyerWaitPay)
    LinearLayout llBuyerWaitPay;
    @BindView(R.id.llSellerWaitPay)
    LinearLayout llSellerWaitPay;
    @BindView(R.id.llSellerWaitCoin)
    LinearLayout llSellerWaitCoin;
    @BindView(R.id.llBuyerWaitCoin)
    LinearLayout llBuyerWaitCoin;
    @BindView(R.id.llAppealing)
    LinearLayout llAppealing;
    @BindView(R.id.llCancel)
    LinearLayout llCancel;
    @BindView(R.id.llComplete)
    LinearLayout llComplete;
    @BindView(R.id.tvStatus)
    TextView tvStatus;
    private boolean isBuyer;  // true 购买者  false 出售者
    @BindView(R.id.tvHeaderControl)
    TextView tvHeaderControl;
    @BindView(R.id.tvHeaderType)
    TextView tvHeaderType;
    @BindView(R.id.tvHeaderNum)
    TextView tvHeaderNum;
    @BindView(R.id.tvOrderNum)
    TextView tvOrderNum;
    @BindView(R.id.tvPrice)
    TextView tvPrice;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.tvAllMoney)
    TextView tvAllMoney;
    @BindView(R.id.tvWeChatAccount)
    TextView tvWeChatAccount;
    @BindView(R.id.llWeChat)
    LinearLayout llWeChat;
    @BindView(R.id.tvAlipayAccount)
    TextView tvAlipayAccount;
    @BindView(R.id.llAlipay)
    LinearLayout llAlipay;
    @BindView(R.id.tvBankAccount)
    TextView tvBankAccount;
    @BindView(R.id.tvBank)
    TextView tvBank;
    @BindView(R.id.llBank)
    LinearLayout llBank;
    @BindView(R.id.tvBankBranch)
    TextView tvBankBranch;
    @BindView(R.id.tvBuyerUnpayTime)
    TextView tvBuyerUnpayTime;
    @BindView(R.id.tvBuyerUnpayTo)
    TextView tvBuyerUnpayTo;
    @BindView(R.id.tvBuyerUnpayNumber)
    TextView tvBuyerUnpayNumber;
    @BindView(R.id.tvSellerWaitCoinNumber)
    TextView tvSellerWaitCoinNumber;
    @BindView(R.id.tvSellerWaitCoinTime)
    TextView tvSellerWaitCoinTime;
    @BindView(R.id.btMakeSureReceiveMoney)
    Button btMakeSureReceiveMoney;
    @BindView(R.id.tvBuyerWaitCoinNumber)
    TextView tvBuyerWaitCoinNumber;
    @BindView(R.id.tvBuyerWaitCoinTime)
    TextView tvBuyerWaitCoinTime;
    @BindView(R.id.tvAppealNumber)
    TextView tvAppealNumber;
    @BindView(R.id.btMarkPay)
    Button btMarkPay;
    @BindView(R.id.btCancel)
    Button btCancel;
    @BindView(R.id.ivWechat)
    ImageView ivWechat;
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.ivAlipay)
    ImageView ivAlipay;
    @BindView(R.id.tvSellerUnpayTo)
    TextView tvSellerUnpayTo;
    @BindView(R.id.tvSellerUnpayTime)
    TextView tvSellerUnpayTime;
    @BindView(R.id.tvSellerUnpayNumber)
    TextView tvSellerUnpayNumber;
    @BindView(R.id.btSellerUnpayCancel)
    Button btSellerUnpayCancel;
    @BindView(R.id.order6btAppeal)
    Button order6btAppeal;  // 冻结中按钮
    @BindView(R.id.tvTradeType)
    TextView tvTradeType;
    @BindView(R.id.pay_type)
    TextView payType;
    @BindView(R.id.tvCompleteNum)
    TextView tvCompleteNum;
    @BindView(R.id.tvPhone)
    TextView tvPhone;
    @BindView(R.id.tvAppealPayNumber)
    TextView tvAppealPayNumber;
    @BindView(R.id.tvBuyerUnpayFirst)
    TextView tvBuyerUnpayFirst;
    @BindView(R.id.detail_content)
    TextView detailContent;
    @BindView(R.id.contactPhone)
    ImageView contactPhone;
    private ArrayAdapter<String> mAdapter;
    private String[] mStringArray;
    private MaterialDialog tipAppeal;
    private OrderDetailBean data;
    private Disposable timerDispo;
    private Disposable detailDispo;
    private BottomSheetDialog pwdDialog;
    private AlertDialog payTipDialog;
    private MaterialDialog tipCancleDialog;
    private ImageView ivQR;
    private TextView tvQR;
    private LinearLayout imgTvLl;
    private AlertDialog qrDialog;
    private HashMap<String, String> payTipMap;
    private String[] stringArray;
    private String orderNum;
    private AlertDialog tipAppealDialog;
    private Spinner spinner;
    private EditText etReason;
    private BottomSheetDialog codeDialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fabi_sell_detail2;
    }

    @Override
    public FabiSellDetail2Presenter getPresenter() {
        return new FabiSellDetail2Presenter();
    }

    public static void start(Context context, String orderNum, boolean isBuy) {
        Intent starter = new Intent(context, FabiSellDetail2Activity.class);
        starter.putExtra(Constants.ORDER_NUM, orderNum);
        starter.putExtra(Constants.IS_BUY, isBuy);
        context.startActivity(starter);
    }

    @Override
    protected void initView() {
        ClickUtil.click(ivBack, () -> {
            finish();
        });
        isBuyer = getIntent().getBooleanExtra(Constants.IS_BUY, true);  // 1 出售 2 购买者
        orderNum = getIntent().getStringExtra(Constants.ORDER_NUM);
        setTitle(App.INSTANCE.getString(R.string.fabiOrderDetailActivity0));
        stringArray = getResources().getStringArray(R.array.fabi_appeal);
        llAlipay.setVisibility(View.GONE);
        llBank.setVisibility(View.GONE);
        llWeChat.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        payTipMap = new HashMap<>();
        payTipMap.put("1", App.INSTANCE.getString(R.string.fabiOrderDetailActivity2));
        payTipMap.put("2", App.INSTANCE.getString(R.string.fabiOrderDetailActivity4));
        mPresenter.getOrderInfo(orderNum, true);
    }

    public void updateUI(OrderDetailBean data) {
        this.data = data;
        Map<String, String> buyerMap = new HashMap<>();
        Map<String, String> sellerMap = new HashMap<>();
        buyerMap.put("1", App.INSTANCE.getString(R.string.fabiOrderDetailActivity7));
        buyerMap.put("2", App.INSTANCE.getString(R.string.fabiOrderDetailActivity9));
        buyerMap.put("3", App.INSTANCE.getString(R.string.fabiOrderDetailActivity11));
        buyerMap.put("4", App.INSTANCE.getString(R.string.fabiOrderDetailActivity13));
        buyerMap.put("5", App.INSTANCE.getString(R.string.fabiOrderDetailActivity15));
        buyerMap.put("6", "冻结中");
        sellerMap.put("1", App.INSTANCE.getString(R.string.fabiOrderDetailActivity17));
        sellerMap.put("2", App.INSTANCE.getString(R.string.fabiOrderDetailActivity19));
        sellerMap.put("3", App.INSTANCE.getString(R.string.fabiOrderDetailActivity11));
        sellerMap.put("4", App.INSTANCE.getString(R.string.fabiOrderDetailActivity13));
        sellerMap.put("5", App.INSTANCE.getString(R.string.fabiOrderDetailActivity15));
        sellerMap.put("6", "冻结中");
//        isBuyer = "1".equals(data.getType()) ? false : true;  // 1 出售 2 购买者
        tvHeaderControl.setText(String.format(App.INSTANCE.getString(R.string.fabiOrderDetailActivity26), data.getOop_name()));
        tvHeaderType.setText(isBuyer ? App.INSTANCE.getString(R.string.fabiOrderDetailActivity27) : App.INSTANCE.getString(R.string.fabiOrderDetailActivity28));
        tvHeaderNum.setText(String.format("%sUSDT", NumberUtil.keep4(data.getTotalNum())));
        tvPhone.setText(TextUtils.isEmpty(data.getOop_mobile()) ? "***" : data.getOop_mobile());

        tvTradeType.setText(isBuyer ? App.INSTANCE.getString(R.string.fabiOrderDetailActivity31) : App.INSTANCE.getString(R.string.fabiOrderDetailActivity32));
        tvOrderNum.setText(data.getDealEntrustNo());
        tvPrice.setText(NumberUtil.keep2(data.getPrice()) + " CNY");
        tvNum.setText(NumberUtil.keep4(data.getTotalNum()) + " USDT");
        tvAllMoney.setText(NumberUtil.keep2(data.getTotalPrice()) + " CNY");
        detailContent.setText(data.getNotes());
        if (data.getStockUserPayWays() != null) {
            Flowable.fromIterable(data.getStockUserPayWays())
                    .subscribe(payType -> {
                        switch (payType.getPayType()) {
                            case "wx":
                                llWeChat.setVisibility(View.VISIBLE);
                                tvWeChatAccount.setText(payType.getNumber());
                                ClickUtil.click(ivWechat, () -> {
                                    showQR(payType);
                                });
                                break;
                            case "alipay":
                                llAlipay.setVisibility(View.VISIBLE);
                                tvAlipayAccount.setText(payType.getNumber());
                                ClickUtil.click(ivAlipay, () -> {
                                    showQR(payType);
                                });
                                break;
                            case "backcard":
                                llBank.setVisibility(View.VISIBLE);
                                tvBank.setText(payType.getBankCardOpenBank());
                                tvBankAccount.setText(payType.getNumber());
                                tvBankBranch.setText(payType.getBankCardType());
                                ClickUtil.click(tvBankAccount, () -> {
                                    CopyUtil.copy(payType.getNumber());
                                });

                                break;
                        }

                    });
        }
        DisposUtil.close(timerDispo);
        if (isBuyer) {//买家页面
//            tvHeaderControl.setText(String.format(App.INSTANCE.getString(R.string.fabiOrderDetailActivity26), data.getSellUsername()));
//            tvHeaderType.setText(App.INSTANCE.getString(R.string.fabiOrderDetailActivity27));
//            tvHeaderType.setTextColor(ContextCompat.getColor(this, R.color.fabi_market_green));
            tvStatus.setText(buyerMap.get(data.getStatus()));
            payType.setText(App.INSTANCE.getString(R.string.fabi_paytype_sell));
            switch (data.getStatus()) {
                case "1"://未支付
                    buyerUnPay();
                    break;
                case "2"://等待放币
                    buyerWaitCoin();
                    break;
                case "4"://卖家申诉中
                    buyerAndSellerAppeal();
                    break;
                case "5"://订单取消
                    orderCancel();
                    break;
                case "3"://订单完成
                    orderComplete();
                    break;
                case "6":  // 订单冻结
                    order6Appeal();
                    break;
            }
        } else {//卖家页面
            tvStatus.setText(sellerMap.get(data.getStatus()));
            payType.setText(App.INSTANCE.getString(R.string.fabi_paytype_buy));
            switch (data.getStatus()) {
                case "1"://待付款
                    sellerUnPay();
                    break;
                case "2"://待放币
                    sellerWaitCoin();
                    break;
                case "4"://卖家申诉中
                    buyerAndSellerAppeal();
                    break;
                case "5"://订单取消
                    orderCancel();
                    break;
                case "3"://订单完成
                    orderComplete();
                    break;
                case "6":  // 订单冻结
                    order6Appeal();
                    break;
            }
        }
    }

    /**
     * 卖家等待放币
     */
    private void sellerWaitCoin() {
        order6btAppeal.setVisibility(View.GONE);
        llSellerWaitPay.setVisibility(View.GONE);
        llBuyerWaitPay.setVisibility(View.GONE);
        llBuyerWaitCoin.setVisibility(View.GONE);
        llSellerWaitCoin.setVisibility(View.VISIBLE);
        llAppealing.setVisibility(View.GONE);
        llCancel.setVisibility(View.GONE);
        llComplete.setVisibility(View.GONE);
        tvSellerWaitCoinNumber.setText(" " + data.getRefer());
        tvSellerWaitCoinTime.setText(getLeftTime(data.getQr_time()));
        timerDispo = Flowable.intervalRange(1, data.getQr_time(), 0, 1, TimeUnit.SECONDS)
                .map(i -> data.getQr_time() - i)
                .compose(RxSchedulersHelper.transformer())
                .subscribe(i -> {
                    tvSellerWaitCoinTime.setText(getLeftTime(i));

                });
        ClickUtil.click(btAppeal, () -> {//申诉
            showAppeal();
        });
        ClickUtil.click(btMakeSureReceiveMoney, () -> {//放币
            showPwdBottom();
        });
        refreshTimerOrder();
    }

    private String getLeftTime(Long i) {
        return i / 60 + getString(R.string.fabi_minute) + i % 60 + getString(R.string.fabi_second);
    }

    /**
     * 显示申诉弹窗
     */
    private void showAppeal() {
        //使用自定义的ArrayAdapter
        mStringArray = getResources().getStringArray(R.array.fabiAppeal);
        mAdapter = new SpinnerTextColorAdapter(FabiSellDetail2Activity.this, mStringArray);
        if (tipAppealDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            View view = View.inflate(this, R.layout.dialog_fabi_sell_detail_appeal, null);
            ClickUtil.click(view.findViewById(R.id.tv_cancel), () -> tipAppealDialog.dismiss());
            ClickUtil.click(view.findViewById(R.id.tv_apply), () -> {
                tipAppealDialog.dismiss();
                String type = spinner.getSelectedItem().toString();
                String reason = etReason.getText().toString();
                mPresenter.allegeOrder(orderNum, data.getRefer(), data.getOop_account(), type, reason);

            });
            builder.setView(view);
            builder.setCancelable(true);
            etReason = view.findViewById(R.id.et_reason);
            spinner = view.findViewById(R.id.spinner);
            spinner.setAdapter(mAdapter);
            tipAppealDialog = builder.create();
        }

        tipAppealDialog.show();
    }

    /**
     * 卖家未支付
     */
    private void sellerUnPay() {
        order6btAppeal.setVisibility(View.GONE);
        llBuyerWaitPay.setVisibility(View.GONE);
        llSellerWaitPay.setVisibility(View.VISIBLE);
        llBuyerWaitCoin.setVisibility(View.GONE);
        llSellerWaitCoin.setVisibility(View.GONE);
        llAppealing.setVisibility(View.GONE);
        llCancel.setVisibility(View.GONE);
        llComplete.setVisibility(View.GONE);
        tvSellerUnpayTo.setText(String.format(App.INSTANCE.getString(R.string.fabiOrderDetailActivity52), data.getOop_name()));
        tvSellerUnpayTime.setText(getLeftTime(data.getDown_time()));
        tvSellerUnpayNumber.setText(App.INSTANCE.getString(R.string.fabiOrderDetailActivity53) + data.getRefer());
        timerDispo = Flowable.intervalRange(1, data.getDown_time(), 0, 1, TimeUnit.SECONDS)
                .map(i -> data.getDown_time() - i)
                .compose(RxSchedulersHelper.transformer())
                .subscribe(i -> {
                    tvSellerUnpayTime.setText(getLeftTime(i));
                });
        ClickUtil.click(btSellerUnpayCancel, () -> {
            showCancelTip();
        });
        refreshTimerOrder();
    }

    /**
     * 订单完成
     */
    private void orderComplete() {
        order6btAppeal.setVisibility(View.GONE);
        llSellerWaitPay.setVisibility(View.GONE);
        llBuyerWaitPay.setVisibility(View.GONE);
        llBuyerWaitCoin.setVisibility(View.GONE);
        llSellerWaitCoin.setVisibility(View.GONE);
        llAppealing.setVisibility(View.GONE);
        llCancel.setVisibility(View.GONE);
        llComplete.setVisibility(View.VISIBLE);
        tvCompleteNum.setText(data.getRefer());
        DisposUtil.close(detailDispo);
    }

    /**
     * 订单取消
     */
    private void orderCancel() {
        order6btAppeal.setVisibility(View.GONE);
        llSellerWaitPay.setVisibility(View.GONE);
        llBuyerWaitPay.setVisibility(View.GONE);
        llBuyerWaitCoin.setVisibility(View.GONE);
        llSellerWaitCoin.setVisibility(View.GONE);
        llAppealing.setVisibility(View.GONE);
        llCancel.setVisibility(View.VISIBLE);
        llComplete.setVisibility(View.GONE);
        DisposUtil.close(detailDispo);

    }

    /**
     * 申诉中
     */
    private void buyerAndSellerAppeal() {
//        contactPhone.setVisibility(View.VISIBLE); // 申诉中拨打对方手机号
//        ClickUtil.click(contactPhone, () -> {
//            if (!TextUtils.isEmpty(data.getOop_mobile())) {
//                if (CheckUtil.isEmail(data.getOop_mobile())) {
//                    CopyUtil.copy(data.getOop_mobile());
//                    ToastUtil.showShort(data.getOop_mobile() + "复制成功");
//                } else {
//                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.getOop_mobile()));
//                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    startActivity(intent);
//                }
//            }
//        });
        order6btAppeal.setVisibility(View.GONE);
        llSellerWaitPay.setVisibility(View.GONE);
        llBuyerWaitPay.setVisibility(View.GONE);
        llBuyerWaitCoin.setVisibility(View.GONE);
        llSellerWaitCoin.setVisibility(View.GONE);
        llAppealing.setVisibility(View.VISIBLE);
        llCancel.setVisibility(View.GONE);
        llComplete.setVisibility(View.GONE);
        tvAppealNumber.setText(data.getCommand());
        tvAppealPayNumber.setText(data.getRefer());
        refreshTimerOrder();

    }

    /**
     * 买家等待放币
     */
    private void buyerWaitCoin() {
        order6btAppeal.setVisibility(View.GONE);
        llSellerWaitPay.setVisibility(View.GONE);
        llBuyerWaitPay.setVisibility(View.GONE);
        llBuyerWaitCoin.setVisibility(View.VISIBLE);
        llSellerWaitCoin.setVisibility(View.GONE);
        llAppealing.setVisibility(View.GONE);
        llCancel.setVisibility(View.GONE);
        llComplete.setVisibility(View.GONE);
        tvBuyerWaitCoinNumber.setText(data.getRefer());
        tvBuyerWaitCoinTime.setText(getLeftTime(data.getQr_time()));
        timerDispo = Flowable.intervalRange(1, data.getQr_time(), 0, 1, TimeUnit.SECONDS)
                .map(i -> data.getQr_time() - i)
                .compose(RxSchedulersHelper.transformer())
                .subscribe(i -> {
                    tvBuyerWaitCoinTime.setText(getLeftTime(i));
                });
        refreshTimerOrder();

    }

    /**
     * 冻结中
     */
    public void order6Appeal() {
        order6btAppeal.setVisibility(View.VISIBLE);
        llSellerWaitPay.setVisibility(View.GONE);
        llBuyerWaitPay.setVisibility(View.GONE);
        llBuyerWaitCoin.setVisibility(View.GONE);
        llSellerWaitCoin.setVisibility(View.GONE);
        llAppealing.setVisibility(View.GONE);
        llCancel.setVisibility(View.GONE);
        llComplete.setVisibility(View.GONE);
        DisposUtil.close(detailDispo);
        ClickUtil.click(order6btAppeal, () -> {//申诉
            showAppeal();
        });

    }

    /**
     * 买家未支付
     */
    private void buyerUnPay() {
        order6btAppeal.setVisibility(View.GONE);
        llSellerWaitPay.setVisibility(View.GONE);
        llBuyerWaitPay.setVisibility(View.VISIBLE);
        llBuyerWaitCoin.setVisibility(View.GONE);
        llSellerWaitCoin.setVisibility(View.GONE);
        llAppealing.setVisibility(View.GONE);
        llCancel.setVisibility(View.GONE);
        llComplete.setVisibility(View.GONE);
        if (Locale.SIMPLIFIED_CHINESE.getLanguage().equals(LanguageUtil.getAppLocale(this).getLanguage())) {
            tvBuyerUnpayFirst.setVisibility(View.VISIBLE);
            tvBuyerUnpayNumber.setText(String.format(App.INSTANCE.getString(R.string.fabiOrderDetailActivity54), data.getRefer()));
            tvBuyerUnpayTime.setText(getLeftTime(data.getDown_time()));
            tvBuyerUnpayTo.setText(String.format(App.INSTANCE.getString(R.string.fabiOrderDetailActivity55), data.getOop_name()));
        } else {
            tvBuyerUnpayFirst.setVisibility(View.GONE);
            tvBuyerUnpayNumber.setText(String.format("Waiting for payment,please pay to %s within %s", data.getOop_name(), getLeftTime(data.getDown_time())));
            tvBuyerUnpayTime.setText("");
            tvBuyerUnpayTo.setText("");
        }
        timerDispo = Flowable.intervalRange(1, data.getDown_time(), 0, 1, TimeUnit.SECONDS)
                .map(i -> data.getDown_time() - i)
                .compose(RxSchedulersHelper.transformer())
                .subscribe(i -> {
                    if (Locale.SIMPLIFIED_CHINESE.getLanguage().equals(LanguageUtil.getAppLocale(this).getLanguage())) {

                        tvBuyerUnpayTime.setText(getLeftTime(i));
                    } else {
                        tvBuyerUnpayNumber.setText(String.format("Waiting for payment,please pay to %s within %s", data.getOop_name(), getLeftTime(i)));
                    }
                });
        ClickUtil.click(btMarkPay, () -> {//标记付款
            showPayTip();
        });
        ClickUtil.click(btCancel, () -> {//取消订单
            showCancelTip();
        });
        refreshTimerOrder();
    }

    /**
     * 定时刷新订单信息
     */
    private void refreshTimerOrder() {
        if (detailDispo == null || detailDispo.isDisposed()) {
            detailDispo = Flowable.interval(0, 5, TimeUnit.SECONDS)
                    .subscribe(aLong -> {
                        mPresenter.getOrderInfo(orderNum, false);
                    });
        }
    }

    /**
     * 显示支付密码底部弹窗
     */
    private void showPwdBottom() {
        if (codeDialog == null) {
            codeDialog = BottomUpUtil.getPay(this, mPresenter, (pwd) -> {
                codeDialog.dismiss();
                mPresenter.letGo(orderNum, pwd);
            });
        }
        codeDialog.show();
    }

    /**
     * 支付完成提示弹窗
     */
    private void showPayTip() {
        if (payTipDialog == null) {
            payTipDialog = TipUtil.customTip1(this, App.INSTANCE.getString(R.string.strFabiSellDetailActivity332),
                    App.INSTANCE.getString(R.string.strFabiSellDetailActivity333),
                    App.INSTANCE.getString(R.string.strFabiSellDetailActivity215), () -> mPresenter.markPay(orderNum));
        }
        payTipDialog.show();
    }
//    /**
//     * 收款提示弹窗
//     */
//    private void showWithdrawTip() {
//        if (payTipDialog == null) {
//            payTipDialog = TipUtil.getTip(this, App.INSTANCE.getString(R.string.fabiOrderDetailActivity59),
//                    App.INSTANCE.getString(R.string.fabiOrderDetailActivity60),
//                    App.INSTANCE.getString(R.string.fabiOrderDetailActivity58), () -> mPresenter.letGo(orderNum, data.getId(), isBuyer ? "1" : "2"));
//        }
//        payTipDialog.show();
//    }

    /**
     * 显示撤单提示
     */
    private void showCancelTip() {
        if (tipCancleDialog == null) {
            tipCancleDialog = TipUtil.getTip(this, App.INSTANCE.getString(R.string.fabiOrderDetailActivity64), () -> {
                mPresenter.cancelOrder(orderNum);

            });

        }
        tipCancleDialog.show();
    }


    /**
     * 显示二维码弹窗
     */
    private void showQR(OrderDetailBean.StockUserPayWaysBean dataItem) {
        if (qrDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            View view = View.inflate(this, R.layout.fabi_dialog_erweima, null);
            ivQR = view.findViewById(R.id.ivQRCode);
            tvQR = view.findViewById(R.id.tvContent);
            imgTvLl = view.findViewById(R.id.img_tv_ll);
            builder.setView(view);
            builder.setCancelable(true);
            qrDialog = builder.create();
        }
        ImageUtil.setImage(dataItem.getImg(), ivQR);
        ClickUtil.click(imgTvLl, () -> {
            new RxPermissions(FabiSellDetail2Activity.this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(isGrant -> {
                        if (isGrant) {
                            if (ImgUtils.saveImageToGallery(FabiSellDetail2Activity.this, ivQR)) {
                                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabi_detail));
                            } else {
                                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabi_detai2));
                            }
                        } else {
                            PermissionTipUtil.tip(FabiSellDetail2Activity.this, App.INSTANCE.getString(R.string.fabi_detai4));
                        }
                    });
        });
//        tvQR.setText(dataItem.getTip() + "收款二维码");
        tvQR.setText(App.INSTANCE.getString(R.string.fabi_detai3));
        qrDialog.show();
    }

    public void refresh() {
        mPresenter.getOrderInfo(orderNum, false);
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(detailDispo);
        super.onDestroy();
    }
}
